package com.leyou.item.inter.fallback;

import com.leyou.common.bean.PageResult;
import com.leyou.common.bean.Result;
import com.leyou.common.bean.ResultCode;
import com.leyou.item.inter.api.IGoodsService;
import com.leyou.item.inter.dto.SpuPageDTO;
import com.leyou.item.inter.dto.SpuSaveDTO;
import com.leyou.item.inter.pojo.Sku;
import com.leyou.item.inter.pojo.Spu;
import com.leyou.item.inter.pojo.SpuDetail;
import com.leyou.item.inter.vo.SpuBoVO;
import com.netflix.hystrix.exception.HystrixTimeoutException;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author zqq
 * @ClassName GoodsServiceFallbackFactory
 * @Description
 * @date 2020/3/7-16:38
 */
@Component
@Slf4j
public class GoodsServiceFallbackFactory implements FallbackFactory<IGoodsService> {
    @Override
    public IGoodsService create(Throwable throwable) {
        return new IGoodsService(){
            @Override
            public Result<PageResult<SpuBoVO>> querySpuBoByPage(SpuPageDTO dto) {
                log.error("fallback,causedby: " + throwable);
                if (throwable != null) {
                    if (throwable instanceof HystrixTimeoutException) {
                        return Result.newFailure(ResultCode.S_REQUEST_TIME_OUT);
                    }
                }
                return Result.newFailure(ResultCode.S_SYSTEM_EXCEPTION);
            }

            @Override
            public Result saveGoods(SpuSaveDTO dto) {
                log.error("fallback,causedby: " + throwable);
                if (throwable != null) {
                    if (throwable instanceof HystrixTimeoutException) {
                        return Result.newFailure(ResultCode.S_REQUEST_TIME_OUT);
                    }
                }
                return Result.newFailure(ResultCode.S_SYSTEM_EXCEPTION);
            }

            @Override
            public Result<SpuDetail> querySpuDetailBySpuId(Long spuId) {
                log.error("fallback,causedby: " + throwable);
                if (throwable != null) {
                    if (throwable instanceof HystrixTimeoutException) {
                        return Result.newFailure(ResultCode.S_REQUEST_TIME_OUT);
                    }
                }
                return Result.newFailure(ResultCode.S_SYSTEM_EXCEPTION);
            }

            @Override
            public Result<List<Sku>> queryEnableSkusBySpuId(Long spuId) {
                log.error("fallback,causedby: " + throwable);
                if (throwable != null) {
                    if (throwable instanceof HystrixTimeoutException) {
                        return Result.newFailure(ResultCode.S_REQUEST_TIME_OUT);
                    }
                }
                return Result.newFailure(ResultCode.S_SYSTEM_EXCEPTION);
            }

            @Override
            public Result<List<Sku>> querySkusBySpuId(Long spuId) {
                log.error("fallback,causedby: " + throwable);
                if (throwable != null) {
                    if (throwable instanceof HystrixTimeoutException) {
                        return Result.newFailure(ResultCode.S_REQUEST_TIME_OUT);
                    }
                }
                return Result.newFailure(ResultCode.S_SYSTEM_EXCEPTION);
            }

            @Override
            public Result updateGoods(SpuSaveDTO dto) {
                log.error("fallback,causedby: " + throwable);
                if (throwable != null) {
                    if (throwable instanceof HystrixTimeoutException) {
                        return Result.newFailure(ResultCode.S_REQUEST_TIME_OUT);
                    }
                }
                return Result.newFailure(ResultCode.S_SYSTEM_EXCEPTION);
            }

            @Override
            public Result<Spu> querySpuById(Long id) {
                log.error("fallback,causedby: " + throwable);
                if (throwable != null) {
                    if (throwable instanceof HystrixTimeoutException) {
                        return Result.newFailure(ResultCode.S_REQUEST_TIME_OUT);
                    }
                }
                return Result.newFailure(ResultCode.S_SYSTEM_EXCEPTION);
            }

            @Override
            public Result<Sku> querySkuById(Long id) {
                log.error("fallback,causedby: " + throwable);
                if (throwable != null) {
                    if (throwable instanceof HystrixTimeoutException) {
                        return Result.newFailure(ResultCode.S_REQUEST_TIME_OUT);
                    }
                }
                return Result.newFailure(ResultCode.S_SYSTEM_EXCEPTION);
            }
        };
    }
}
